package Apps;

import java.sql.*;


public class CreateDataBase{
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    static private Connection con = null;

    static {
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection("jdbc:hive2://master:10000/default", "", "");

        } catch (SQLException e) {
            System.out.println("None连接模式运行出错，现在尝试NOSASL连接模式...");
            try {
                con = DriverManager.getConnection("jdbc:hive2://master:10000/default;auth=noSasl",
                        "hiveuser", "");//NOSASL
            } catch (SQLException e1) {
                e.printStackTrace();
                e1.printStackTrace();
            }

        }
    }


    private void createDataBase(String dbName) throws SQLException {
        Statement stmt = con.createStatement();
        String sql = "CREATE DATABASE " + dbName;
        System.out.println("Running: " + sql);
        stmt.execute(sql);
        System.out.println("成功");
    }

    public static void main( String[] args )
    {
        try {
            new CreateDataBase().createDataBase("userdb4");


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("error");
        }
    }
}